package com.hwh.blog.web.admin;

import com.hwh.blog.po.Blog;
import com.hwh.blog.po.User;
import com.hwh.blog.qu.BlogQuery;
import com.hwh.blog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogServiceImpl service;
    @Autowired
    private TypeServiceImpl typeService;
    @Autowired
    private TagServiceImpl tagService;
    @Autowired
    private CommentServiceImpl commentService;
    @GetMapping("/blogs")
    public String GotoBlogs(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC)  Pageable pageable, BlogQuery blog, Model model){
        model.addAttribute("page",service.listBlog(pageable,blog));
        model.addAttribute("types",typeService.listType());
        return "admin/blogs";
    }
    @RequestMapping("/blogs/search")
    public String Serarch(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model){
        model.addAttribute("page",service.listBlog(pageable,blog));
        return "admin/blogs :: blogList";
    }
    @RequestMapping("/blogs/input")
    public String InputBlog(Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog",new Blog());
        return "admin/blogs-inputs";
    }
    @RequestMapping("/blogs")
    public String Post(Blog blog , RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getIds()));
        Blog b;
        if (blog.getId() == null) {
            b =  service.saveBlog(blog);
        } else {
            b = service.updateBlog(blog.getId(), blog);
        }
        if (b == null ) {
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }
        return "redirect:/admin/blogs";
    }
    @RequestMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        Blog b = service.getBlog(id);
        b.init();
        model.addAttribute("blog", b);
        return "admin/blogs-inputs";
    }
    @GetMapping("/blogs/{id}/delete")
    public String deleteBlogs(@PathVariable Long id,RedirectAttributes attributes){
        commentService.deleteComment(id);
        service.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/blogs";
    }
}
